#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using Thn;
#endregion

namespace Thn.Communication.Teleporting
{
    /// <summary>
    /// Simplified and transportable exception information class
    /// </summary>
    public class TeleportExceptionInfo
    {
        #region Message
        private string mMessage = string.Empty;
        /// <summary>
        /// Gets/Sets error message
        /// </summary>
        [Savable]
        public string Message
        {
            get { return mMessage; }
            set { mMessage = value; }
        }
        #endregion

        #region Exception
        private string mException = string.Empty;
        /// <summary>
        /// Gets/Sets type name of exception
        /// </summary>
        [Savable]
        public string Exception
        {
            get { return mException; }
            set { mException = value; }
        }
        #endregion

        #region Inner Exception
        private string mInnerException = string.Empty;
        /// <summary>
        /// Gets/Sets type inner exception (if available)
        /// </summary>
        [Savable]
        public string InnerException
        {
            get { return mInnerException; }
            set { mInnerException = value; }
        }
        #endregion

        #region Inner Message
        private string mInnerMessage = string.Empty;
        /// <summary>
        /// Gets/Sets message of inner exception (if available)
        /// </summary>
        [Savable]
        public string InnerMessage
        {
            get { return mInnerMessage; }
            set { mInnerMessage = value; }
        }
        #endregion

        #region Stack Trace
        private string mStackTrace = string.Empty;
        /// <summary>
        /// Gets/Sets stack trace
        /// </summary>
        [Savable]
        public string StackTrace
        {
            get { return mStackTrace; }
            set { mStackTrace = value; }
        }
        #endregion

        #region Build Message
        /// <summary>
        /// Flatten info into string
        /// </summary>
        public string BuildMessage()
        {
            string s = "";

            s = mMessage + "\r\nType: " + mException + "\r\nMessage: " + mMessage + "\r\nStack Trace: \r\n" + mStackTrace;
            if (mInnerException != "")
            {
                s += "\r\n\r\nInner Exception Type: " + mInnerException + "\r\nInner Exception Message: " + mInnerMessage;
            }
            #region Defensive tracing
            else
            {
                //not available
            }
            #endregion

            return s;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public TeleportExceptionInfo()
        { }

        /// <summary>
        /// Create a new instance around an exception
        /// </summary>
        public TeleportExceptionInfo(Exception error)
        {
            mException = error.GetType().Name;
            mMessage = error.Message;
            mStackTrace = error.StackTrace;

            if (error.InnerException != null)
            {
                mInnerException = error.InnerException.GetType().Name;
                mInnerMessage = error.InnerException.Message;
                mStackTrace += "\r\nInner:\r\n" + error.InnerException.StackTrace;
            }
            #region Defensive tracing
            else
            {
                //not available
            }
            #endregion
        }
        #endregion
    }
}
